//
//  ZNChatCell.m
//  ZNChatDemo
//
//  Created by zhengn on 16/3/18.
//  Copyright © 2016年 Sniper_Personal. All rights reserved.
//

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define edgeTOP 10
#define edgeLEFT 10
#define edgeBOTTOM 10
#define edgeRIGHT 10

#import "ZNChatCell.h"

@interface ZNChatCell ()

@property (weak, nonatomic) IBOutlet UIButton *headImg;
@property (weak, nonatomic) IBOutlet UIButton *messageContent;
@property (weak, nonatomic) IBOutlet UIButton *yourHeadImg;
@property (weak, nonatomic) IBOutlet UIButton *urMessageContent;

@end

@implementation ZNChatCell

+ (instancetype)chatCell {
    ZNChatCell *chatCell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    return chatCell;
}

-(void)setMessage:(ZNMessage *)message {
    _message = message;
    
    if (message.fromWho == ZNMessageSenderTypeFromMe) {
        self.headImg.hidden = NO;
        self.messageContent.hidden = NO;
        self.yourHeadImg.hidden = YES;
        self.urMessageContent.hidden = YES;
        [self.messageContent setTitle:_message.messageContent forState:UIControlStateNormal];
        [self.headImg setImage:[UIImage imageNamed:_message.headImgUrl] forState:UIControlStateNormal];
        
        [self.messageContent layoutIfNeeded];
        CGFloat messageContentHeight = self.messageContent.titleLabel.frame.size.height;
        
        CGRect btnRect = self.messageContent.frame;
        CGRect btnNewRect = CGRectMake(btnRect.origin.x, btnRect.origin.y, btnRect.size.width, messageContentHeight);
        self.messageContent.frame = btnNewRect;
        [self.messageContent layoutIfNeeded];
        
        CGFloat maxMessage = CGRectGetMinY(self.messageContent.frame) + messageContentHeight + edgeTOP + edgeBOTTOM;
        CGFloat maxHeadImg = CGRectGetMaxY(self.headImg.frame);
        message.cellHeight = MAX(maxMessage, maxHeadImg) + 10;

    } else if (message.fromWho == ZNMessageSenderTypeFromYou) {
        self.yourHeadImg.hidden = NO;
        self.urMessageContent.hidden = NO;
        self.headImg.hidden = YES;
        self.messageContent.hidden = YES;
        [self.urMessageContent setTitle:_message.messageContent forState:UIControlStateNormal];
        [self.yourHeadImg setImage:[UIImage imageNamed:_message.headImgUrl] forState:UIControlStateNormal];
        
        [self.urMessageContent layoutIfNeeded];
        CGFloat messageContentHeight = self.urMessageContent.titleLabel.frame.size.height ;
        
        CGRect btnRect = self.urMessageContent.frame;
        CGRect btnNewRect = CGRectMake(btnRect.origin.x, btnRect.origin.y, btnRect.size.width, messageContentHeight);
        self.urMessageContent.frame = btnNewRect;
        [self.urMessageContent layoutIfNeeded];
        
        CGFloat maxMessage = CGRectGetMinY(self.urMessageContent.frame) + messageContentHeight + edgeTOP + edgeBOTTOM;
        CGFloat maxHeadImg = CGRectGetMaxY(self.yourHeadImg.frame);
        message.cellHeight = MAX(maxMessage, maxHeadImg) + 10;

    }
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect btnRect = self.messageContent.frame;
    btnRect.size.height = self.messageContent.titleLabel.frame.size.height + edgeTOP + edgeBOTTOM;
    self.messageContent.frame = btnRect;
    
    CGRect urbtnRect = self.urMessageContent.frame;
    urbtnRect.size.height = self.urMessageContent.titleLabel.frame.size.height + edgeTOP + edgeBOTTOM;
    self.urMessageContent.frame = urbtnRect;
}

- (void)setup {
    self.backgroundColor = [UIColor clearColor];
    
    self.messageContent.titleLabel.numberOfLines = 0;
    self.messageContent.titleLabel.preferredMaxLayoutWidth = 240;
    [self.messageContent setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.messageContent.contentEdgeInsets = UIEdgeInsetsMake(edgeTOP, edgeLEFT, edgeBOTTOM, edgeRIGHT);
//    self.messageContent.layer.borderWidth = 1;
    
    self.urMessageContent.titleLabel.numberOfLines = 0;
    self.urMessageContent.titleLabel.preferredMaxLayoutWidth = 240;
    [self.urMessageContent setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.urMessageContent.contentEdgeInsets = UIEdgeInsetsMake(edgeTOP, edgeLEFT, edgeBOTTOM, edgeRIGHT);
//    self.urMessageContent.layer.borderWidth = 1;
}

- (void)awakeFromNib {
    [self setup];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
